package main;
import gameobjects.Ant;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

import main.Map.State;
import gameobjects.Cake;
import gameobjects.Ground;
import gameobjects.Wall;


class DrawPanel extends JPanel{
		public Map map;
		State curState;
		private int cellSize = GameMain.CANVAS_HEIGHT/Map.MAP_DIM;
		
		public DrawPanel(){
			map = new Map();
			curState = State.WALL;
			addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e){
					setCell(e.getX(), e.getY());
				}
			});
			
			addMouseMotionListener(new MouseMotionAdapter(){
					public void mouseDragged(MouseEvent e){
						setCell(e.getX(), e.getY());	
					}
			});
			repaint();
		}
		
		private void setCell(int x, int y){
			int numbX = x/cellSize;
			int numbY = y/cellSize;
			if ((numbX < 0) || (numbX > Map.MAP_DIM - 1) || (numbY < 0) || (numbY > Map.MAP_DIM - 1)) return;
			switch (curState) {
				case WALL:
					map.mat[numbX][numbY].isFree = false;
					map.mat[numbX][numbY].mobID = new Wall(new Point(numbX, numbY));
					break;
				case CAKE:
					map.mat[numbX][numbY].isFree = false;
					map.mat[numbX][numbY].mobID = new Cake(new Point(numbX, numbY));
					break;
				default:
					break;
			}
			repaint();
		}
		
		private void drawGrid(Graphics g){
			g.setColor(Color.GRAY);
			for(int i  = 0; i <= Map.MAP_DIM; i++){
					g.drawLine(i*cellSize, 0, i*cellSize, Map.MAP_DIM*cellSize);
					g.drawLine(0, i*cellSize, Map.MAP_DIM*cellSize, i*cellSize);
				}
		}
		
		public void cleanMap(){
			map.clear();
			repaint();
		}
		
		public void setState(State ident){
			curState = ident;
		}
		
		@Override
		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			for(int i = 0; i < Map.MAP_DIM; i++)
				for(int j = 0; j < Map.MAP_DIM; j++){
					switch (map.mat[i][j].mobID.getType()) {
						case GROUND:
							g.setColor(new Color(255,222,165));
							break;
						case WALL:
							g.setColor(new Color(169,118,0));
							break;
						case CAKE:
							g.setColor(new Color(255,0,0));
							break;
						default:
							break;
				}
				g.fillRect(i*cellSize, j*cellSize, cellSize, cellSize);
			}		
			drawGrid(g);
		}
		
		public static void loadMap(String path){
			for(int i = 0; i < Map.MAP_DIM; i++)
				for(int j = 0; j < Map.MAP_DIM; j++){
					switch (0) {
					case 0:
						break;
					case 1:
						break;
					case 2:
						break;
					case 3:
						break;
					case 4:
						break;
					default:
						break;	
					}
				}		
		}
		
		private void saveMap(String name, Map map){
			for(int i = 0; i < Map.MAP_DIM; i++)
				for(int j = 0; j < Map.MAP_DIM; j++){
					switch (map.mat[i][j].mobID.getType()) {
					case GROUND:
						break;
					case WALL:
						break;
					case BLUE_ANT:
						break;
					case GREEN_ANT:
						break;
					case RED_ANT:
						break;
					default:
						break;
							
					}	
				}		
		}
		
	}

 
